import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  File image;
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await ImagePicker.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future<bool> _requestPermissions(PermissionGroup permission) async {
    await PermissionHandler().requestPermissions([permission]);
    PermissionStatus status =
        await PermissionHandler().checkPermissionStatus(permission);
    return status == PermissionStatus.granted;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: <Widget>[
            FlatButton(
              child: Text("选择图片"),
              onPressed: () async {
                var ok = await _requestPermissions(PermissionGroup.photos);
                if (!ok) {
                  return;
                }
                image = await ImagePicker.pickImage(
                    source: ImageSource.gallery,
                    color: "#FFFFFF",
                    isHead: true,
                    language: "en");
                print(image.path);
                setState(() {});
              },
            ),
            FlatButton(
              child: Text("选择图片"),
              onPressed: () async {
                var ok = await _requestPermissions(PermissionGroup.photos);
                if (!ok) {
                  return;
                }
                image = await ImagePicker.pickImage(
                    source: ImageSource.gallery,
                    color: "#FF1B1F39",
                    isEnglishLanguage: true,
                    language: "zh",
                    isHead: true);
                print(image.path);
                setState(() {});
              },
            ),
            FlatButton(
              child: Text("拍照"),
              onPressed: () async {
                var ok = await _requestPermissions(PermissionGroup.camera);
                if (!ok) {
                  return;
                }
                image = await ImagePicker.pickImage(
                    source: ImageSource.camera,
                    color: "#FFFFFF",
                    isHead: true,
                    language: "zh");
                print(image.path);
                setState(() {});
              },
            ),
            image == null
                ? Container(
                    width: 1,
                    height: 1,
                  )
                : Image.file(
                    image,
                    fit: BoxFit.cover,
                    width: 80,
                    height: 80,
                  ),
            Image.file(
              File("/storage/emulated/0/DCIM/camera/IMG_20200403_152510.jpg"),
              fit: BoxFit.cover,
              width: 80,
              height: 80,
            )
          ],
        ),
      ),
    );
  }
}
